#!/usr/bin/perl

require "../std.pl";
require "dates.pl";
%in = getcgivars();

use CGI; 
$query = new CGI; 
$name     = $query->param('name');
$rq_email = $query->param('rq_email');
$obs      = $query->param('obs');
$email    = $query->param('email');
$phone    = $query->param('phone');
$proj     = $query->param('proj');
$where    = $query->param('where');
$st_time  = $query->param('st_time');
$st_date  = $query->param('st_date');
$e_time   = $query->param('e_time');
$e_date   = $query->param('e_date');
$comments = $query->param('comments');
$inDoY = getDOY($st_date, $st_time);
if ($inDoY != -1)
{
    $st_date = fmtDate($st_date);
}

$outDoY = getDOY($e_date, $e_time);
if ($outDoY != -1)
{
    $e_date = fmtDate($e_date);
}

print "Content-type: text/html\n\n";  

open IN,("$wwwroot/includefiles/" . $hfprefix . "header-cgi.html") or die "Failed to open header";
while(<IN>){print "$_";}
close IN;

$title="Remote Observing Input";
print $query->header; 
print "<html><head><title>$title</title>\n";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/ca_style.css\">\n";
print "</head>\n";
print "<body>\n"; 
print "<!--#include virtual=\"/includefiles/ca_header.html\" -->  \n";

$title = "<h1>Remote Observing Request</h1>";

$ok = 0; 

if (($name eq "") || ($rq_email eq "") || ($obs eq "") || ($email eq "") || ($proj eq "") ||
    ($st_time eq "") || ($st_date eq "") || ($e_time eq "") || ($e_date eq ""))
{
    # Print out a message asking that all fields get filled in. 
    print "<font color='red'>\n";
    print "$title\n"; 
    print "<p>Your request for remote observing has not been submitted as you have not filled out all fields\n</p>";
    print "<p>Please return to the request page and complete the form </p>\n";
    print "</font>\n";
} 
elsif (($st_time eq "hh:mm") || ($st_date eq "dd-mmm-yyyy") || ($inDoY == -1) ||
       ($e_time eq "hh:mm")  || ($e_date eq "dd-mmm-yyyy")  || ($outDoY == -1))
{
    # Print out a message indicating that the observer has requested remote observing. 
    print "<font color='red'>\n";
    print "$title\n"; 
    print "<p>Invalid date and time fields.\n</p>";
    print "<p>Please return to the request page and complete the form </p>\n";
    print "</font>\n";
} 
else 
{
    $ok = 1;
    print "$title\n"; 
    print "<p>Your request to observe project $proj remotely on \n";
    print " $st_date has been forwarded to ATNF staff. </p>\n";
    print "<p>Approval (or otherwise) should be granted within three work days \n";
    print " (Australian Time) and a confirmation e-mail sent to you. </p>\n\n";
}

print "<!--#include virtual=\"/includefiles/ca_footer.html\" -->\n";
print "</body></html>\n";
trailer();

###################################################################################################
# read in text file that will have this data added to.                                            #
# load data structure                                                                             #
#   this should have some sort of lock, so to handle two people trying change it at the same time #
###################################################################################################

if ($ok)          # send mail message
{
    $text = "$name ($rq_email) has requested remote observations:\n\n";
    $text .= "Details:\n";
    $text .= "   Project:          $proj\n";
    $text .= "   Observer:         $obs ($email)\n";
    if ($where ne "ATNF Marsfield") 
    {
        $text .= "   Location:         $where\n";
    }
    $text .= "   Contact Phone No: $phone\n";
    $text .= "   Start:            $st_time, $st_date\n   End:              $e_time, $e_date\n";
    if ($comments ne "") 
    {
        $text .= "   Extra information: $comments";
    }
    
    $htmlStr = "<tr><td>$st_time</td>";
    ($p0, $p1, $p2, $p3) = parsDate($st_date);
    $htmlStr .="<td>$p1</td>";
    $htmlStr .="<td>-</td>";
    $htmlStr .="<td>$e_time</td>";
    ($p0, $p1, $p2, $p3) = parsDate($e_date);
    $htmlStr .="<td>$p1 $p2, $p3</td>";
#    $htmlStr .="<td>$e_date</td>";
    $htmlStr .="<td>$proj</td>";
    $htmlStr .="<td>$obs";
    if ($where ne "ATNF Marsfield") 
    {
        $htmlStr .= "<em><font color=maroon> (from $where) </font></em>";
    }
    $htmlStr .= "</td></tr>\n";

#    $distribution = " robin.wark\@csiro.au philip.edwards\@csiro.au Balt.Indermuehle\@csiro.au";
    $distribution = "ste616@gmail.com";

    open PIPE,"| /usr/sbin/sendmail -bm $distribution";
    print PIPE "From: rem_obs\@atnf.csiro.au\n";
    print PIPE "Subject: ATCA Remote Observing Request - $proj on $st_date\n\n";
    print PIPE "$text\n";
    print PIPE "$htmlStr\n";
    close PIPE;
    EOF
}













